/**
 * 广告
 */
import api from '../../tools/api'
import {SUCCESS_STATUS} from '../../config/status'
import promise from '../../tools/promise'
import {
  ADVERT_LIST,
  ADVERT_INFO,
  ADVERT_TREE,
  ADVERT_CLASS_INFO,
  ADVERT_SAVE_DATA,
  ADVERT_COVER
} from '../mutation-types'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  advertList: [],
  advertInfo: {},
  classTree: [],
  classInfo: {},
  saveData: {},
  cover: {}
}

const mutations = {
  [ADVERT_LIST] (state, payload) {
    state.advertList = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [ADVERT_INFO] (state, payload) {
    state.advertInfo = payload
  },
  [ADVERT_TREE] (state, payload) {
    state.classTree = payload
  },
  [ADVERT_CLASS_INFO] (state, payload) {
    state.classInfo = payload
  },
  [ADVERT_SAVE_DATA] (state, payload) {
    state.saveData = payload
  },
  [ADVERT_COVER] (state, data) {
    state.cover = data
  }
}

const actions = {
  // 获取广告列表
  async advertList ({commit}, payload) {
    let listRes = await api('serveradv/adv/Adv/advClassList', payload)
    commit(ADVERT_LIST, listRes.res)
  },
  // 启用广告
  async advertStart ({commit}, payload) {
    let startRes = await api('serveradv/adv/Adv/enableAdv', payload)
    return promise(startRes)
  },
  // 禁用广告
  async advertStop ({commit}, payload) {
    let stopRes = await api('serveradv/adv/Adv/disableAdv', payload)
    return promise(stopRes)
  },
  // 审核通过广告
  async advertPass ({commit}, payload) {
    let stopRes = await api('serveradv/adv/Adv/advAudit', payload)
    return promise(stopRes)
  },
  // 审核失败广告
  async advertFailed ({commit}, payload) {
    let stopRes = await api('serveradv/adv/Adv/advAuditFailure', payload)
    return promise(stopRes)
  },
  // 删除广告
  async advertDel ({commit}, payload) {
    let delRes = await api('serveradv/adv/Adv/deleteAdv', payload)
    return promise(delRes)
  },
  // 获取广告详情
  async advertInfo ({commit}, payload) {
    let infoRes = await api('serveradv/adv/Adv/advinfo', payload)
    if (SUCCESS_STATUS === infoRes.status) {
      commit(ADVERT_INFO, infoRes.res)
    }
  },
  // 获取广告分类树
  async classTree ({commit}, payload) {
    let treeRes = await api('serveradv/adv/AdvClassify/gainAdvClassifyList', payload)
    if (SUCCESS_STATUS === treeRes.status) {
      commit(ADVERT_TREE, treeRes.res)
    }
  },
  // 启用广告分类
  async classStart ({commit}, payload) {
    let startRes = await api('serveradv/adv/AdvClassify/enableAdvClassify', payload)
    return promise(startRes)
  },
  // 禁用广告分类
  async classStop ({commit}, payload) {
    let stopRes = await api('serveradv/adv/AdvClassify/disableAdvClassify', payload)
    return promise(stopRes)
  },
  // 删除广告分类
  async classDel ({commit}, payload) {
    let delRes = await api('serveradv/adv/AdvClassify/deleteAdvClassify', payload)
    return promise(delRes)
  },
  // 添加广告分类
  async classAdd ({commit}, payload) {
    let addRes = await api('serveradv/adv/AdvClassify/addAdvClassify', payload)
    return promise(addRes)
  },
  // 获取广告分类详情
  async classInfo ({commit}, payload) {
    let infoRes = await api('serveradv/adv/AdvClassify/classifyInfo', payload)
    if (SUCCESS_STATUS === infoRes.status) {
      commit(ADVERT_CLASS_INFO, infoRes.res)
    }
  },
  // 编辑广告分类
  async classEdit ({commit}, payload) {
    let editRes = await api('serveradv/adv/AdvClassify/editAdvClassify', payload)
    return promise(editRes)
  },
  // 添加广告
  async advertAdd ({commit}, payload) {
    let addRes = await api('serveradv/adv/Adv/addAdv', payload)
    return promise(addRes)
  },
  // 编辑广告
  async advertEdit ({commit}, payload) {
    let editRes = await api('serveradv/adv/Adv/editAdv', payload)
    return promise(editRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
